package com.ruoyi.vote.common.strategy;

import com.ruoyi.common.enums.UserType;
import com.ruoyi.common.exception.vote.VoteException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.vote.admin.domain.VoteActivity;
import com.ruoyi.vote.common.utils.KeyUtils;
import com.ruoyi.vote.common.utils.VoteCheckUtils;

/**
 * 活动上下文类
 */
public class ActivityContext {
    private static ActivityCheckStrategy activityStatus;

    /**
     * 设置当前活动的状态
     * @param activityStatus  校验环境
     */
    public static void setActivityStatus(ActivityCheckStrategy activityStatus) {
        ActivityContext.activityStatus = activityStatus;
    }

    /**
     * 调用当前活动状态的校验
     * @param eventId  活动redisKey
     * @return
     * @throws VoteException
     */
    public static VoteActivity checkActivity(String eventId) throws VoteException {
        String infoEventKey = KeyUtils.formatInfoEventKey(eventId);
        VoteActivity activity = VoteCheckUtils.activityCheck(infoEventKey);
        if (activityStatus != null && UserType.XCX_MEMBER.equals(LoginHelper.getUserType())) {
            activityStatus.check(activity);
        }
        return activity;
    }
}
